/**
 * _ooOoo_
 * o8888888o
 * 88" . "88
 * (| -_- |)
 * O\  =  /O
 * ____/`---'\____
 * .'  \\|     |//  `.
 * /  \\|||  :  |||//  \
 * /  _||||| -:- |||||-  \
 * |   | \\\  -  /// |   |
 * | \_|  ''\---/''  |   |
 * \  .-\__  `-`  ___/-. /
 * ___`. .'  /--.--\  `. . __
 * ."" '<  `.___\_<|>_/___.'  >'"".
 * | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * \  \ `-.   \_ __\ /__ _/   .-` /  /
 * ======`-.____`-.___\_____/___.-`____.-'======
 * `=---='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * 佛祖保佑        永无BUG
 */
package com.inossem.emergency;

import android.app.Application;
import android.content.Context;

import com.inossem.emergency.utils.crashException.CrashExceptionUtils;
import com.inossem.emergency.utils.logger.LogUtils;
import com.inossem.emergency.utils.logger.LogWriter;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import org.xutils.x;

import me.jessyan.autosize.AutoSizeConfig;
import me.jessyan.autosize.unit.Subunits;


/**
 * Application
 *
 * @author 詹建宇
 * @time on 2019/2/13 10:10
 * @email jianyu.zhan@inossem.com
 */
public class InossemApplication extends Application {
    private static InossemApplication context;

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//        MultiDex.install(this);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        //xUtils3初始化
        x.Ext.init(this);
        context = this;

        //初始化屏幕适配工具
        initDensityHelper();
        //logger
        initLogger();
        // 初始化异常捕获
        initCrashException(context);
    }

    private void initDensityHelper() {
//        ScreenAdapterTools.init(this);
//        DensityHelper.initHelper(this, DensityHelper.WIDTH_1920P);
        AutoSizeConfig.getInstance().getUnitsManager()
                .setSupportDP(false)
                .setSupportSP(false)
                .setSupportSubunits(Subunits.PT);
    }

    private void initLogger() {
        Logger.addLogAdapter(new AndroidLogAdapter());
    }

    private void initCrashException(Context context) {
        CrashExceptionUtils.initCrashException(context, (Context c) -> {
            LogWriter.saveLog(context, LogUtils.LOG_NAME_CRASHEXCEPTION, "未捕获异常造成崩溃，进入统一错误捕捉"
                    + LogUtils.NEW_LINE + LogUtils.exceptionToString(null));
            exitApp();
        });
    }

    //============================================public============================================

    /**
     * 获取Context
     */
    public static InossemApplication getContext() {
        return context;
    }


    /**
     * 退出程序
     */
    public void exitApp() {
        android.os.Process.killProcess(android.os.Process.myPid()); // 杀死进程
        System.exit(0);// 直接结束程序
    }

}
